#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder);
TreeNode *ConstructSubTree(vector<int>::const_iterator inBegin, vector<int>::const_iterator inEnd,
    vector<int>::const_iterator postBegin, vector<int>::const_iterator postEnd);

int main(int argc, char** argv){




    return 0;
}

TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder){
    return ConstructSubTree(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
}

TreeNode *ConstructSubTree(vector<int>::const_iterator inBegin, vector<int>::const_iterator inEnd,
                           vector<int>::const_iterator postBegin, vector<int>::const_iterator postEnd){
    if(inBegin == inEnd){
        return NULL;
    }

    TreeNode *pRoot = new TreeNode(*(postEnd-1));
    vector<int>::const_iterator iRootIn = find(inBegin, inEnd, pRoot->val);
    // left subtree
    if(iRootIn != inBegin){
        // has left subtree
        pRoot->left = ConstructSubTree(inBegin, iRootIn, postBegin, postBegin+(iRootIn-inBegin));
    }
    // right subtree
    if(iRootIn != (inEnd-1)){
        // has right subtree
        pRoot->right = ConstructSubTree(iRootIn+1, inEnd, postBegin+(iRootIn-inBegin), postEnd-1);
    }
    return pRoot;
}